package file;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Saver {

	public static void save(String message) {
		// extract name and message and append to correct file.
		message = message.substring(18);
		String name = "";
		if (message.charAt(0) == 'e' || message.charAt(0) == 'E') {
			name = "erik";
			message = message.substring(4);
		} else {
			name = "johan";
			message = message.substring(5);
		}
		
		try {
			FileWriter fw = new FileWriter("notes/" + name + ".txt", true);
			fw.write("\n" + getDate() + "\n");
			fw.write(message + "\n<--->");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static String getDate() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(Calendar.getInstance().getTime());
	}
}
